import requests
from lxml import etree
if __name__ == '__main__':
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
    }
    # 爬取到页面源码数据
    url = 'https://sz.58.com/ershoufang/'
    page_text = requests.get(url=url, headers=headers).text

    # 数据解析
    tree = etree.HTML(page_text)
    # 存储的h3标签
    list1 = tree.xpath('//div[@class="property"]/a')

    fp = open('58.txt', 'w', encoding='utf-8')
    for h3 in list1:
        # 局部解析
        title = h3.xpath('./div[2]/div/div/h3/text()')[0]
        fp.write(title+'\n')